import { createPinia } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate'
import { storageCrypto } from '@/utils/crypto'

const pinia = createPinia()


// 根据环境变量配置加密
const isEncrypt = import.meta.env.VITE_ENCRYPT === 'true'

// 使用持久化存储插件
pinia.use(createPersistedState({
    storage: localStorage,
    ...(isEncrypt && {
        serializer: {
            serialize: (value) => storageCrypto.encrypt(value),
            deserialize: (value) => storageCrypto.decrypt(value)
        }
    })
}))

// 自动注册模块
const modules = import.meta.glob('./modules/**.js', { eager: true })

Object.values(modules).forEach(module => {
    module.default?.()
})

export default pinia



// 模块统一导出
export * from './modules/system'
export * from './modules/user'
export * from './modules/auth'